package com.central.apps.service.impl;

import com.central.app.service.HandlerService;
import com.central.apps.service.DocStatisticsService;
import com.central.apps.service.TaskStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 交付物接口实现类
 *
 * @author 李大超
 * @date 2023/1/30
 */
@Slf4j
@Service
public class DocStatisticsServiceImpl implements DocStatisticsService {

    @Autowired
    private HandlerService handlerService;

    /**
     * 交付物统计
     *
     * @param proid
     */
    @Override
    public void docStatistics(Integer proid) throws Exception {
        int proTotalDocs = 0;
        int docTotalFinish = 0;
        int docTotalOngoing = 0;
        int docTotalDelay = 0;
        List<Map<String, Object>> list = handlerService.findBySql("select count(id) as proTotalDocs, ifnull(sum((case when status = '已交付' then 1 else 0 end)), 0) as docTotalFinish, " +
                " ifnull(sum((case when status = '未交付' then 1 else 0 end)), 0) as docTotalOngoing, " +
                " ifnull(sum((case when status = '已延期' then 1 else 0 end)), 0) as docTotalDelay from gq_prooutputplandetail where proid = "+ proid);

        if (CollectionUtils.isNotEmpty(list)) {
            Map<String, Object> map = list.get(0);
            proTotalDocs = MapUtils.getInteger(map, "proTotalDocs");
            docTotalFinish = MapUtils.getInteger(map, "docTotalFinish");
            docTotalOngoing = MapUtils.getInteger(map, "docTotalOngoing");
            docTotalDelay = MapUtils.getInteger(map, "docTotalDelay");

            handlerService.updateBySql("update gq_proaccount set prototaldocs = " + proTotalDocs
                    + ", doctotalfinish = " + docTotalFinish
                    + ", doctotalongoing = " + docTotalOngoing
                    + ", doctotaldelay = " + docTotalDelay
                    + " where id = "+ proid);
        }
    }
}
